﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ServiceModel;
using System.ServiceProcess;
using System.Configuration;
using System.Configuration.Install;
using System.ComponentModel;

namespace WCFTimesheetService
{
    class TimeSheetService : ServiceBase
    {
        public ServiceHost serviceHost = null;

        public TimeSheetService()
    {
        // Name the Windows Service
        ServiceName = "TimesheetService";
    }

    public static void Mainold()
    {
        ServiceBase.Run(new TimeSheetService());
    }

    // Start the Windows service.
    protected override void OnStart(string[] args)
    {
        if (serviceHost != null)
        {
            serviceHost.Close();
        }

        // Create a ServiceHost for the CalculatorService type and 
        // provide the base address.
        Uri address = new Uri(Properties.Settings.Default.Address);
        serviceHost = new ServiceHost(typeof(ValidateTimesheet),  address );

        // Open the ServiceHostBase to create listeners and start 
        // listening for messages.
        serviceHost.Open();
    }

    protected override void OnStop()
    {
        if (serviceHost != null)
        {
            serviceHost.Close();
            serviceHost = null;
        }
    }

    // Provide the ProjectInstaller class which allows 
    // the service to be installed by the Installutil.exe tool
    [RunInstaller(true)]
    public class ProjectInstaller : Installer
    {
        private ServiceProcessInstaller process;
        private ServiceInstaller service;

        public ProjectInstaller()
        {
            process = new ServiceProcessInstaller();
            process.Account = ServiceAccount.LocalSystem;
            service = new ServiceInstaller();
            service.ServiceName = "TimesheetService";
            Installers.Add(process);
            Installers.Add(service);
        }
    }

    }
}
